using System;
using System.Collections;
using System.Web;

namespace app.scrum
{
    public partial class SprintDetail2 : ScrumPage
    {

        public SprintDetail2()
        {
            this.sObject.tableName = "backlog";
        }

        public override void read(HttpContext context)
        {            
            this.sObject.readCmdText = "select b.* from backlog__c b, sprint__c s"
                    + " where b.sprint__c = s.id__c";
            try
            {
                ArrayList sObjList = this.sObject.read();
                ArrayList statusList = Constant.Status.values;
                string statusCandidateValues = JsonHelper.stringList2Array(statusList);
                ScrumObjectHelper.assignCandidateValues(sObjList, "status", statusCandidateValues);
                ArrayList userList = this.sObject.getScrumObjectSimpleList("user__c");
                string userJson = JsonHelper.getJson(userList);
                string userJsonRecPart = JsonHelper.getJsonRecordPartFromJson(userJson);
                string userCandiateValues = JsonHelper.jsonRecPart2Array(userJsonRecPart);
                ScrumObjectHelper.assignCandidateValues(sObjList, "assignee", userCandiateValues);
                ScrumObjectHelper.assignDisplay(sObjList, "assignee", userList);
                ScrumObjectHelper.assignLookup(this.sObject, sObjList, "team__c");
                ScrumObject task = new ScrumObject();
                task.tableName = "task__c";
                task.readCmdText = "select * from task__c";
                ArrayList taskList = task.read();
                foreach (ScrumObject backlogObj in sObjList)
                {
                    foreach (ScrumObject taskObj in taskList)
                    {
                        ScrumField backlogIdField = (ScrumField)backlogObj.fieldMap["id__c"];
                        ScrumField taskBacklogField = (ScrumField)taskObj.fieldMap["backlog__c"];
                        if (backlogIdField.value.Equals(taskBacklogField.value))
                        {
                            if (backlogObj.childMap[task.tableName] == null)
                            {
                                backlogObj.childMap.Add(task.tableName, new ArrayList());
                            }
                            ArrayList tempList = (ArrayList)backlogObj.childMap[task.tableName];
                            tempList.Add(taskObj);
                            backlogObj.childList.Add(taskObj);
                        }
                    }
                }
                string json = JsonHelper.getJson(sObjList);
                json = JsonHelper.getJsonRootPartValueFromJson(json);
                context.Response.Write(json);
            }
            catch (Exception ex)
            {
                this.handleException(context.Response, ex);
            }
        }
    }
}